

#import "JPMessageCell.h"

@interface JPMessageCell ()

@property(weak,nonatomic)UILabel *timeLabel;
@property(weak,nonatomic)UIImageView *iconImage;
@property(weak,nonatomic)UIButton *textButton;

@end

@implementation JPMessageCell

+ (instancetype)messageCellWithTableView:(UITableView *)tableView {
    static NSString *rid = @"msg";
    JPMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:rid];
    
    if (cell == nil) {
        cell = [[JPMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rid];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 创建控件
        UILabel *timeLabel = [UILabel new];
        [self.contentView addSubview:timeLabel];
        
        timeLabel.textAlignment = NSTextAlignmentCenter;
        timeLabel.font = [UIFont systemFontOfSize:12];
        self.timeLabel = timeLabel;
        
        UIImageView *img = [UIImageView new];
        [self.contentView addSubview:img];
        self.iconImage = img;
        
        UIButton *textBtn = [UIButton new];
        [self.contentView addSubview:textBtn];
        // 换行
        textBtn.titleLabel.numberOfLines = 0;
        // 文字颜色
        [textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        textBtn.titleLabel.font = TEXT_FONT;
        
        textBtn.contentEdgeInsets =UIEdgeInsetsMake(20,20,20,20);
        
        self.textButton = textBtn;
    }
    return self;
}

/**
 *  当有数据传进来之后,会自动调用此方法
 */
- (void)setMsgF:(JPMessageFrame *)msgF {
    _msgF = msgF;
    // 设置子控件的frame
    [self setSubViewsFrame];
    // 设置子控件的数据
    [self setSubViewsData];
}
- (void)setSubViewsFrame {
    self.timeLabel.frame =self.msgF.timeF;
    self.iconImage.frame = self.msgF.iconF;
    self.textButton.frame = self.msgF.textF;
}
- (void)setSubViewsData {
    JPMessageModel *msgM = self.msgF.msgM;
    self.timeLabel.text = msgM.time;
    
    if (msgM.type == MessageTypeMe) {
        self.iconImage.image = [UIImage imageNamed:@"me"];
    } else {
        self.iconImage.image = [UIImage imageNamed:@"other"];
    }
    
    [self.textButton setTitle:msgM.text forState:UIControlStateNormal];
    UIImage *imgNor = nil;
    UIImage *imgHigh = nil;
    
    if (msgM.type == MessageTypeMe) {
        
        imgNor = [UIImage imageNamed:@"chat_send_nor"];
        imgHigh = [UIImage imageNamed:@"chat_send_press_pic"];
    } else {
        
        imgNor = [UIImage imageNamed:@"chat_recive_nor"];
        imgHigh = [UIImage imageNamed:@"chat_recive_press_pic"];
    }
    // 准备四根线的位置
    CGFloat topNor = imgNor.size.height / 2;
    CGFloat leftNor = imgNor.size.width / 2;
    CGFloat bottomNor = imgNor.size.height / 2;
    CGFloat rightNor = imgNor.size.width / 2;
    // 就是切割图片的方法
    // 第一个参数：就是设置四条线的位置
    // 第二个参数：设置中间部分填充的模式
    // 注意：这个方法会产生一个新的Image对象，所以如果你需要它改变必须重新接收一下
    imgNor = [imgNor resizableImageWithCapInsets:UIEdgeInsetsMake(topNor, leftNor, bottomNor, rightNor) resizingMode:UIImageResizingModeTile];
    // 准备四根线的位置
    CGFloat topHigh = imgHigh.size.height / 2;
    CGFloat leftHigh = imgHigh.size.width / 2;
    CGFloat bottomHigh = imgHigh.size.height / 2;
    CGFloat rightHigh = imgHigh.size.width / 2;
    // 就是切割图片的方法
    // 第一个参数：就是设置四条线的位置
    // 第二个参数：设置中间部分填充的模式
    // 注意：这个方法会产生一个新的Image对象，所以如果你需要它改变必须重新接收一下
    imgHigh = [imgHigh resizableImageWithCapInsets:UIEdgeInsetsMake(topHigh, leftHigh, bottomHigh, rightHigh) resizingMode:UIImageResizingModeTile];
    
    [self.textButton setBackgroundImage:imgNor forState:UIControlStateNormal];
    [self.textButton setBackgroundImage:imgHigh forState:UIControlStateHighlighted];
    
}
@end
